import {Component, OnInit} from '@angular/core';
import {changeTheme, ThemeKey} from './util/app.util';
import {NavigationEnd, Router} from '@angular/router';
import {ConfigService} from './service/config.service';
import {UserService} from './service/user.service';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css'],
})
export class AppComponent implements OnInit {

  constructor(
    private router: Router,
    private userService: UserService, private configService: ConfigService) {
    this.router.events.subscribe(event => {
      if (event instanceof NavigationEnd) {
        // 如果没获取用户配置才加载
        if (this.userService.user && this.configService.firstRun) {
          this.configService.firstRun = false;
          this.configService.userConfigEvent.emit(null);
        }
        // 执行完动画后改变属性,防止滚动出现问题
        setTimeout(() => {
          const page = document.getElementById('main').children.item(1) as HTMLElement;
          page.style.position = '';
        }, 800);
      }
    });
  }

  ngOnInit(): void {
    if (localStorage.getItem(ThemeKey)) {
      changeTheme(localStorage.getItem(ThemeKey));
    }
  }
}
